package com.quick.android.utils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

public class JsonUtils {
	/** 将json解析为javabean */
	@SuppressWarnings("finally")
	public static <T> T parseString(String json, Class<T> clazz) {
		if (TextUtils.isEmpty(json)) {
			return null;
		}
		T obj = null;
		try {
			JSONObject jsonObj = new JSONObject(json);
			obj = clazz.getConstructor().newInstance();
			Field fields[] = clazz.getDeclaredFields();
			int size = fields.length;
			for (int i = 0; i < size; i++) {
				Field field = fields[i];
				field.setAccessible(true);
				String fieldName = field.getName();
				String className = field.getType().getSimpleName();
				if (jsonObj.has(fieldName)) {
					if (TextUtils.equals(className, "String")) {
						field.set(obj, jsonObj.getString(fieldName));
					} else if (TextUtils.equals(className, "int")) {
						field.setInt(obj, jsonObj.getInt(fieldName));
					} else if (TextUtils.equals(className, "double")) {
						field.setDouble(obj, jsonObj.getDouble(fieldName));
					} else if (TextUtils.equals(className, "long")) {
						field.setLong(obj, jsonObj.getLong(fieldName));
					} else if (TextUtils.equals(className, "boolean")) {
						field.setBoolean(obj, jsonObj.getBoolean(fieldName));
					} else {
						// 判断 是否为List对象
						if (List.class.isAssignableFrom(field.getType())) {
							List list = new ArrayList();
							Type type = field.getGenericType();
							if (type instanceof ParameterizedType) {
								// 获取类型的类型参数类型。
								// 你可以去查看jdk帮助文档对ParameterizedType的解释。
								JSONArray array = jsonObj
										.getJSONArray(fieldName);
								Class childClazz = (Class) ((ParameterizedType) type)
										.getActualTypeArguments()[0];
								if (array.length() > 0) {
									int length = array.length();
									for (int j = 0; j < length; j++) {
										if (TextUtils.equals(
												childClazz.getSimpleName(),
												"String")) {
											list.add(array.getString(j));
										} else {
											list.add(parseString(array.getString(j),
													childClazz));
										}
									}
								}
								field.set(obj, list);
							}

						} else {
							// 判断是否我 JaveBean对象
							field.set(
									obj,
									parseString(jsonObj.getString(fieldName),
											field.getType()));
						}
					}
				}

			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			return obj;
		}
	}

	/** javabean toString()作用 */
	public static <T> String parseBean(T obj) {
		if (obj == null) {
			return "";
		}
		StringBuilder sb = new StringBuilder("{");
		Field fields[] = obj.getClass().getFields();
		if (fields.length > 0) {
			int length = fields.length;
			Field field = null;
			for (int i = 0; i < length; i++) {
				field = fields[i];
				field.setAccessible(true);
				String fieldName = field.getName();
				String className = field.getType().getSimpleName();
				try {
					if (TextUtils.equals(className, "String")) {
						sb.append(fieldName + ":" + (String) field.get(obj)
								+ ",");
					} else if (TextUtils.equals(className, "int")) {
						sb.append(fieldName + ":" + field.getInt(obj) + ",");
					} else if (TextUtils.equals(className, "double")) {
						sb.append(fieldName + ":" + field.getDouble(obj) + ",");
					} else if (TextUtils.equals(className, "long")) {
						sb.append(fieldName + ":" + field.getLong(obj) + ",");
					} else if (TextUtils.equals(className, "boolean")) {
						sb.append(fieldName + ":" + field.getBoolean(obj) + ",");
					} else if (TextUtils.equals(className, "float")) {
						sb.append(fieldName + ":" + field.getFloat(obj) + ",");
					} else if (TextUtils.equals(className, "short")) {
						sb.append(fieldName + ":" + field.getShort(obj) + ",");
					} else {
						if (List.class.isAssignableFrom(field.getType())) {// 判断是否为List对象
							List list = (List) field.get(obj);
							if (list != null && list.size() > 0) {
								StringBuilder mStrList = new StringBuilder();
								int size = list.size();
								for (int j = 0; j < size; j++) {
									mStrList.append(j + ":"
											+ parseBean(list.get(j)) + ",");
								}
								mStrList.deleteCharAt(mStrList.length() - 1);
								sb.append(fieldName + ":["
										+ mStrList.toString() + "],");
							} 
						} else {
							if (field.get(obj) != null)
								sb.append(fieldName + ":"
										+ parseBean(field.get(obj)) + ",");
						}
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}

			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append("}");
		}

		return sb.toString();
	}

}
